$(function() {
	/*
	 * 加载user
	 */
	$(document).ready(function() {
		loadUser();
	});
	/*
	 * 搜索user
	 */
	$(document).on('click',"#searchUser",function(){
		loadUser();
	});
	/*
	 * 重置密码
	 */
	$(document).on("click","#updatePassword",function() {
		var id = $(this).attr("value");
        findUser(id);
	});
});

function loadUser() {
	var url = ctx+"/user/list";
	var condition1 = $("#condition1").val();
	var json = {pageable:"pageable",condition1:condition1};
	var role = $("#role").val();
	var a = "管理员";
	var b ="普通用户";
	jqList.query(url,json,function(data){
		if(data.code==200) {
			var itemsHtml = '';
			var len = data.list.length;
			if(len==0) {
				
				itemsHtml='<tr><td colspan="8">无数据...</td></tr> ';
				
			}
			if(role==1) {
			for(var i=0;i<len;i++) {
				var u = data.list[i];
				if(u.role==1) {
					r = a;
				}
				if(u.role==2) {
					r=b;
				}
				(function(){
					itemsHtml += '<tr>'+
					             '<td>'+(1+i)+'</td>'+
					             '<td>'+u.username+'</td>'+
					            
					              '<td>'+r+'</td>'+
					                '<td><a id="updatePassword" class="btn btn-info btn-sm" value="'+u.id+'" href="javascript:void(0);" data-toggle="modal" data-target="#updateUserModal" style="float:left;">重置</a>'+
					              '<a class="btn btn-danger btn-sm" onClick="delUser('+u.id+')" style="float: right;">删除</a>'+'</td>'+
					              
					             '</tr>'
				})(i);
			}
		}
			if(role==2) {
			for(var i=0;i<len;i++) {
				var u = data.list[i];
				if(u.role==1) {
					r = a;
				}
				if(u.role==2) {
					r=b;
				}
				(function(){
					itemsHtml += '<tr>'+
					             '<td>'+(1+i)+'</td>'+
					             '<td>'+u.username+'</td>'+
					             
					              '<td>'+r+'</td>'+
					                
				                  '</tr>'
				})(i);
              
			}
		 }
			$('#userList').html(itemsHtml);
		}
	});
}

/*
 * 查询要更新的用户
 */
function findUser(id) { 
	$.ajax({
		type: 'POST',
		url: ctx + '/user/findUser',
		data: {id:id},
		dataType:'json',
		success:function(data){
			if(data.code == 200){
				var user = data.user;
				$("#userId").val(id);
				$("#username1").val(user.username);
			}
		}
	})
};

/*
 * 删除
 */
function delUser(id){
	confirm_tip("确定删除？",function(flag){
		if(flag==1){
			$.ajax({
				type: 'POST',
				dataType:'json',
				url: ctx + '/user/delete',
				data: {id:id},
				success:function(data){
					if(data.code == 200){
						window.location.href= ctx + '/user';
					}
				}
			});
		}
	});
}


